		// create angular app
			var ProfileApp = angular.module('profile', []);
			
			// We use the directive to recompile for
			ProfileApp.directive('compileTemplate', function($compile, $parse){
			    return {
			        link: function(scope, element, attr){
			            var parsed = $parse(attr.ngBindHtml);
			            function getStringValue() { return (parsed(scope) || '').toString(); }

			            //Recompile if the template changes
			            scope.$watch(getStringValue, function() {
			                $compile(element, null, -9999)(scope);  //The -9999 makes it skip directives so that we do not recompile ourselves
			            });
			        }         
			    }
			});
			
			// create angular controller
			ProfileApp.controller('getUserDetails', function($scope,$http,$sce) {
				$scope.loadData = function(){
					$http.post("profile").then(function(response) {
		                    	var responseArray = response.data.split(",");
		                    	$scope.nickname = responseArray[0];
		                    	$scope.mail = responseArray[1];
		                    	$scope.description = responseArray[2];
		                    	$scope.photo = responseArray[3];
		                    	$scope.username = responseArray[4];
		                    	if (responseArray[5] == "noButton"){
		                    		$scope.EnableFollowButton = false;
		                    		$scope.EnableUnFollowButton = false;
		                    	}
		                    	else if (responseArray[5] == "Follow"){
		                    		$scope.EnableFollowButton = true;
		                    		$scope.EnableUnFollowButton = false;                    	
		                    	}
		                    	else if (responseArray[5] == "UnFollow"){
		                    		$scope.EnableFollowButton = false;
		                    		$scope.EnableUnFollowButton = true;	                    		
		                    	}
		                    });
				}
				
				
				$scope.checkSession = function(wnd){					
						$http.post("sessioncheck", {Type:wnd}).then(function(response) 
						{
							var array = response.data;
					    	if (response.data.Answer == "Failed")
							{
					    		var l = response.data.Location;
					    		window.location = l.toString();
					    	}
					    	else{
					    		$scope.loadData();
					    	}
				        });
				};
				
				
				
				
				$scope.btnFollow= function(username)
				{
					$http.post("followuser", {UsernameToFollow:username}).error(function(response) {
                    	alert("Error");
					}).then(function(response){
                		$scope.EnableFollowButton = false;
                		$scope.EnableUnFollowButton = true;
					});
				}
				
				$scope.btnUnFollow= function(username)
				{
					$http.post("unfollowuser", {UsernameToFollow:username}).error(function(response) {
						alert("Error");
					}).then(function(response){
                		$scope.EnableFollowButton = true;
                		$scope.EnableUnFollowButton = false;
					});
				
				}

	
				$scope.btnFollowMember= function(follower_username, currentuser_username, type)
				{
					$http.post("followuser", {UsernameToFollow:follower_username}).error(function(response) {
                    	alert("Error");
					}).then(function(response){
						if(type == "followings") $scope.getFollowings(currentuser_username);
						else $scope.getFollowers(currentuser_username);
					});
				}
				
				$scope.btnUnFollowMember= function(follower_username, currentuser_username, type)
				{
					$http.post("unfollowuser", {UsernameToFollow:follower_username}).error(function(response) {
						alert("Error");
					}).then(function(response){
						if(type == "followings") $scope.getFollowings(currentuser_username);
						else $scope.getFollowers(currentuser_username);
					});
				
				}
				
				$scope.getFollowers = function(username)
				{	
					$http.post("getfollowers", {Username:username}).then(function(response){
						$scope.followersList = [];
						for (x in response.data)
						{
							$scope.followersList.push( response.data[x] );
						}
						$scope.ShowFollowersPanel=true;
						$scope.ShowMessagesPanel=false;
						$scope.ShowFollowingsPanel=false;
                    });
					
					
				};

				$scope.getFollowings = function(username)
				{	
					$http.post("getfollowings", {Username:username}).then(function(response){
						$scope.followingsList = [];
						for (x in response.data)
						{
							$scope.followingsList.push( response.data[x] );
						}
						$scope.ShowFollowersPanel=false;
						$scope.ShowMessagesPanel=false;
						$scope.ShowFollowingsPanel=true;
                    });
					
					
				};
				
				$scope.getMessages = function(username,nickname)
				{	
					$http.post("getmsgs", {Type:4, Username:username, Nickname:nickname}).then(function(response){
						$scope.typeofGetMsgs = 4; // all
						$scope.result = [];
						for (x in response.data)
						{
							$scope.result.push( response.data[x] );
							$scope.result[x].theComments = $scope.getCommentPerMessage( response.data[x] );
						}
						$scope.ShowFollowersPanel=false;
						$scope.ShowMessagesPanel=true;
						$scope.ShowFollowingsPanel=false;
                    });
					
					
				};
				
				$scope.messageinsert = function()
				{
					var str = $scope.message_text;
					if(str.length > 140)
					{
						$scope.err= "Text length need to be up to 140";
					}
					else{
						$http.post("messageinsert", {Text:$scope.message_text ,Type:1}).then(function(response) {
							
							var Newid = parseInt( response.data.substr(1 ,(response.data.length)-2) );

							// to add to the begining of the messages
								var temp = {id:Newid,Nickname:$scope.Nick,Text:$scope.message_text,Time:"Posted Now",Comments:0};
								$scope.result.unshift(temp);
								
								//$scope.btnDiscover($scope.typeofGetMsgs); // update the message without restart
								$scope.message_text =""; // reset message text
	
						});
					}

				};
				$scope.btnRepublish = function(x)
				{
					$http.post("messageinsert", {Text:x.Text ,Type:2,MsgID:x.id}).then(function(response) {
						var Newid = parseInt( response.data.substr(1 ,(response.data.length)-2) );
						var NewText = "RE: "+ x.Text;
						// to add to the begining of the messages
						var temp = {id:Newid,Nickname:$scope.Nick,Text:NewText,Time:"Posted Now",Comments:0};
						$scope.result.unshift(temp);
						//$scope.btnDiscover($scope.typeofGetMsgs);// update the message without restart

					});
				}
				$scope.btnComment=function(x)
				{
					x.showComments = !x.showComments;
					
				};
				$scope.getCommentPerMessage = function(x)
				{
					var tempComments = [];
					//var param= $scope.buildStringToGetMsgsComments(r);
					$http.post("getcomments", {id:x.id}).then(function(response){
						
						for (x in response.data)
						{
							tempComments.push( response.data[x] );
						}
						
						
                    });
					return tempComments;
					
				}
				$scope.btnReply = function(x)
				{
					var saveid=x.id;
					//reply to msg id
					$http.post("replymsgs", {"MessageID" : x.id, "Text" : x.message_text_reply}).then( function(response){
						
						$scope.btnDiscover($scope.typeofGetMsgs); // update the message without restart
						x.message_text_reply = "";
					});
					
				};
				
				$scope.btnShowMemberProfile = function(Nickname)
				{	
					//View a member's profile
					$http.post("viewmyprofile", {"Nickname" : Nickname}).then( function(response){
						window.location="User.html"
					});
				}
				
				$scope.TurnToHtml = function(text){
					return ($sce.trustAsHtml(text));
				}
				
				$scope.btnTopicsLoad = function(topic){
					//View a member's profile
					$http.post("settopic", {"Text" : topic}).success( function(response){
						window.location = "Topic.html";
					});
				}
			});
			